package com.laicar.section4_8_textviewcustom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends android.support.v7.widget.AppCompatTextView {
    private float strokeWidth;
    private Integer strokeColor;
    private Paint.Join strokeJoin;
    private float strokeMiter;

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init(attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if(attrs!=null){
            TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CustomTextView);
            if(a.hasValue(R.styleable.CustomTextView_strokeColor)){
                float strokeWidth = a.getDimensionPixelSize(R.styleable.CustomTextView_strokeWidth,1);
                int strokeColor = a.getColor(R.styleable.CustomTextView_strokeColor,0xff000000);
                float strokeMiter = a.getDimensionPixelSize(R.styleable.CustomTextView_strokeMiter,10);
                Paint.Join strokeJoin = null;
                switch (a.getInt(R.styleable.CustomTextView_strokeJoinStyle,0)){
                    case 0:
                        strokeJoin= Paint.Join.MITER;
                        break;
                    case 1:
                        strokeJoin= Paint.Join.BEVEL;
                        break;
                    case 2:
                        strokeJoin= Paint.Join.ROUND;
                        break;
                }
                this.setStroke(strokeWidth,strokeColor,strokeJoin,strokeMiter);
            }
        }
    }

    private void setStroke(float width, int color, Paint.Join join, float miter) {
        this.strokeWidth=width;
        this.strokeColor=color;
        this.strokeJoin=join;
        this.strokeMiter = miter;
    }

    public CustomTextView(Context context) {
        super(context);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int restoreColor = this.getCurrentTextColor();
        if(strokeColor!=null){
            TextPaint paint=this.getPaint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(strokeJoin);
            paint.setStrokeMiter(strokeMiter);
            this.setTextColor(strokeColor);
            paint.setStrokeWidth(strokeWidth);
            super.onDraw(canvas);
            paint.setStyle(Paint.Style.FILL);
            this.setTextColor(restoreColor);

        }
    }
}
